<?php
/**
 * Created by JetBrains PhpStorm.
 * User: quangvu
 * Date: 5/23/13
 * Time: 4:09 PM
 * To change this template use File | Settings | File Templates.
 */

class MemcachedService {

    private static $memcached = null ;

    private function __construct(){}

    public static function getMemcached() {
        if(self::$memcached == null) {
            self::$memcached = new Memcached();
            self::$memcached->addServer(MEMC_HOST, MEMC_PORT);
            self::setOption(self::$memcached);
        }
        return self::$memcached;
    }

    private static function setOption($memcached) {
        $memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, MEMC_OPT_LIBKETAMA_COMPATIBLE);
        $memcached->setOption(Memcached::OPT_DISTRIBUTION, MEMC_OPT_DISTRIBUTION );
        $memcached->setOption(Memcached::OPT_NO_BLOCK, MEMC_OPT_NO_BLOCK );
        $memcached->setOption(Memcached::OPT_CONNECT_TIMEOUT, MEMC_OPT_CONNECT_TIMEOUT);
        $memcached->setOption(Memcached::OPT_RETRY_TIMEOUT, MEMC_OPT_RETRY_TIMEOUT);
        $memcached->setOption(Memcached::OPT_SEND_TIMEOUT, MEMC_OPT_SEND_TIMEOUT);
        $memcached->setOption(Memcached::OPT_RECV_TIMEOUT, MEMC_OPT_RECV_TIMEOUT);
        $memcached->setOption(Memcached::OPT_POLL_TIMEOUT, MEMC_OPT_POLL_TIMEOUT);
        $memcached->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, MEMC_OPT_SERVER_FAILURE_LIMIT);
        $memcached->setOption(Memcached::OPT_COMPRESSION, MEMC_OPT_COMPRESSION );
        $memcached->setOption(Memcached::OPT_TCP_NODELAY, MEMC_OPT_TCP_NODELAY);
    }
}